package com.hjm.app.activity;

import android.content.Intent;
import android.util.Log;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hjm.app.R;
import com.hjm.app.fragment.home.FragmentHome;
import com.hjm.app.fragment.home.FragmentHomeShopCart;
import com.hjm.app.fragment.home.FragmentHomeUserIntro;
import com.hjm.java.entity.UserInfo;
import com.hjm.java.utils.HjmFailCallBack;
import com.hjm.java.utils.HjmListCallBack;
import com.hjm.java.utils.HjmObjectCallBack;
import com.hjm.java.utils.HjmSuccessCallBack;
import com.hjm.ui.common.TopBar;
import com.hjm.ui.goods.adapter.GoodsItemAdapter;
import com.hjm.ui.shopcart.entity.ShopCartVO;

public class MainActivity extends BaseActivity implements
        FragmentHomeShopCart.FragmentHomeShopCartListener,
        GoodsItemAdapter.Listener,
        FragmentHomeUserIntro.OnUserInfoListener {

    private static final String TAG = "MainActivity";
    public static final String FRAGMENT_HOME = "1";
    public static final String FRAGMENT_SHOP_CART = "2";
    public static final String FRAGMENT_USER_INTRO = "3";
    private String type;
    BottomNavigationView mNavView;
    private FragmentHome mFragmentHome;
    private FragmentHomeShopCart mFragmentHomeShopCart;
    private FragmentHomeUserIntro mFragmentHomeUserIntro;
    private TopBar topBar;

    @Override
    void initData() {
        mainService.getGoodsList(6, () -> {
            mFragmentHome.setmGoodsList(mainService.getGoodsListVOList(), this);
        });
        mainService.getStoreNavListVO((isSuccess, t, message) -> {
            if (isSuccess)
                mFragmentHome.setHomeNav(t);
            else
                Log.e(TAG, "initData: "+message);
        });
        // 添加首页片段
        mFragmentHome = new FragmentHome();
        mFragmentList.add(mFragmentHome);
        // 添加购物车片段
        mFragmentHomeShopCart = new FragmentHomeShopCart(this);
        mFragmentList.add(mFragmentHomeShopCart);
        // 添加个人信息片段
        mFragmentHomeUserIntro = new FragmentHomeUserIntro(this);
        mFragmentList.add(mFragmentHomeUserIntro);

        if (type != null && type.equals(FRAGMENT_SHOP_CART)) {
            mNavView.setSelectedItemId(R.id.navigation_cart);
        } else {
            switchFragmentPosition(0);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent: 获取新的intent");
        type = intent.getStringExtra("type");
        if (type != null && type.equals(FRAGMENT_SHOP_CART)) {
            mNavView.setSelectedItemId(R.id.navigation_cart);
        }
    }

    @Override
    int getView() {
        return R.layout.activity_main;
    }

    @Override
    void initView() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        if (type == null) {
//            intent = new Intent(this, GoodsDetailActivity.class);
//            intent.putExtra("storeId",1);
//            intent.putExtra("goodsId",4);
//            startActivity(intent);
        }

        // 实例化组件
        mNavView = findViewById(R.id.nav_view);
        mNavView.setOnItemSelectedListener((item) -> {
            if (item.getItemId() == R.id.navigation_home) {
                Log.d(TAG, "onNavigationItemSelected: 点击首页");
                topBar.setTitle(getResources().getString(R.string.app_name));
                switchFragmentPosition(0);
                return true;
            } else if (item.getItemId() == R.id.navigation_cart) {
                Log.d(TAG, "onNavigationItemSelected: 点击购物车页");
                topBar.setTitle("购物车");
                switchFragmentPosition(1);
                return true;
            } else if (item.getItemId() == R.id.navigation_me) {
                Log.d(TAG, "onNavigationItemSelected: 点击个人信息页");
                topBar.setTitle("个人信息");
                switchFragmentPosition(2);
                return true;
            }
            return false;
        });
        topBar = findViewById(R.id.top_bar);
    }

    @Override
    public void updateShopCartItem(int orderId, int num, HjmSuccessCallBack callBack, HjmFailCallBack failCallBack) {
        mainService.updateShopCartItem(orderId, num, callBack, failCallBack);
    }

    @Override
    public void deleteShopCartItem(int orderId, HjmSuccessCallBack callBack, HjmFailCallBack failCallBack) {
        mainService.deleteShopCartItem(orderId, callBack, failCallBack);
    }

    @Override
    public void getShopCartData(HjmListCallBack<ShopCartVO> listCallBack) {
        mainService.getShopCartData(listCallBack);
    }

    @Override
    public void shopCartPay(int[] orderIds) {
        Log.d(TAG, "shopCartPay: 开始创建订单 " + orderIds.length);
        Intent intent = new Intent(this, OrderActivity.class);
        intent.putExtra(OrderActivity.TYPE, OrderActivity.TYPE_ORDER_CREATE);
        intent.putExtra(OrderActivity.ORDER_IDS, orderIds);
        startActivity(intent);
    }

    @Override
    public void onClick(int id) {
        Log.d(TAG, "onClick: 点击了商品 id=" + id);
        Intent intent = new Intent(this, GoodsDetailActivity.class);
        intent.putExtra("goodsId", id);
        startActivity(intent);
    }

    @Override
    public void getUserInfo(HjmObjectCallBack<UserInfo> callBack) {
        userService.getUserInfo(callBack);
    }
}